//index.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js")
Page({
  data: {
    adlist1: [],
    adlist2: [],
    newslist:[{ID:0,Title:'商城上线公告'}],
    menulist: [{ Name: '菜单', Icon: '', Path: '' }, { Name: '菜单', Icon: '', Path: '' }, { Name: '菜单', Icon: '', Path: '' }, { Name: '菜单', Icon: '', Path: '' }],
    modulelist:[],
    quanlist: [{ ID: 1, Money: 5, Man: 99, EndDate: '2018-05-30 00:00' }, { ID: 2, Money: 10, Man: 199, EndDate: '2018-06-30 00:00' }, { ID: 3, Money: 20, Man: 299, EndDate: '2018-08-30 00:00' }],
    active1:{},
    active2: {},
    active3: {},
    active4: {},
    admininfo:{}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    this.Init_Index();
  },
  Init_Index: function () {
    var that = this;
    var data = {};
    data.act = "Init_Index";
    wx.showLoading({
      title: '加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      var alist = json.data.activelist;
      var active1 = {}, active2 = {}, active3 = {}, active4 = {};
      for (var i = 0; i < alist.length; i++){
        if(alist[i].Position==1){
          active1=alist[i];
        }
        if (alist[i].Position == 2) {
          active2 = alist[i];
        }
        if (alist[i].Position == 3) {
          active3 = alist[i];
        }
        if (alist[i].Position == 4) {
          active4 = alist[i];
        }
      }
      app.AdminInfo=json.data.admin;
      that.setData({
        admininfo: json.data.admin,
        adlist1: json.data.adlist1,
        adlist2: json.data.adlist2,
        newslist: json.data.newslist,
        menulist:json.data.menulist,
        modulelist: json.data.modulelist,
        quanlist:json.data.quanlist,
        active1: active1,
        active2: active2,
        active3: active3,
        active4: active4,
      });
    });
  },
  onPullDownRefresh: function () {
    this.Init_Index();
  },
  openProduct: function (e) {
    app.Keyword = "";
    app.IsFlash=true;
    var path = e.currentTarget.dataset.path;
    wx.navigateTo({
      url: path
    })
  },
  openProductInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/productinfo/productinfo?id=' + id
    })
  },
  openSearch: function () {
    wx.navigateTo({
      url: '/pages/search/search?froms=index'
    })
  },
  openNewsInfo: function (e) {
    wx.navigateTo({
      url: '/pages/newsinfo/newsinfo?id=' + e.currentTarget.dataset.id
    })
  },
  openAd: function (e) {
    var froms = e.currentTarget.dataset.froms;
    if (froms == 1) {
      wx.navigateTo({
        url: '/pages/adinfo/adinfo?id=' + e.currentTarget.dataset.id
      })
    } else if(froms == 2){
      wx.navigateTo({
        url: '/pages/newsinfo/newsinfo?id=' + e.currentTarget.dataset.sid
      })
    } else if (froms == 3) {
      wx.navigateTo({
        url: '/pages/productinfo/productinfo?id=' + e.currentTarget.dataset.sid
      })
    }
  },
  openQuan:function(e){
    var id = e.currentTarget.dataset.id;
    var data = {};
    data.act = "Save_MyQuan";
    data.quanid = id;
    wx.showLoading({ title: '领取中...' });
    app.Ajax(data, function (json) {
      wx.hideLoading();
      if (json.state) {
        app.showSuccess(json.msg);
      }else{
        app.showError(json.msg);
      }
    });
  },
  callPhone: function (e) {
    var phone = e.currentTarget.dataset.value;
    if (phone.trim() != "") {
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }
  },
  onShareAppMessage: function () {
    return {
      path: '/pages/index/index', //这里拼接需要携带的参数
      success: function (res) {
        util.Success("转发成功");
      }
    }
  }
})
